package com.task.manager.mapper;

import com.task.manager.domain.Task;
import com.task.manager.domain.TaskResult;

import java.util.List;

import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

public class TaskResultSqlProvider {

    public String insertSelective(TaskResult record) {
        SQL sql = new SQL();
        sql.INSERT_INTO("t_task_result");
        
        if (record.getId() != null) {
            sql.VALUES("ID", "#{id,jdbcType=INTEGER}");
        }
        
        if (record.getTaskCode() != null) {
            sql.VALUES("TASK_CODE", "#{taskCode,jdbcType=VARCHAR}");
        }
        
        if (record.getTaskName() != null) {
            sql.VALUES("TASK_NAME", "#{taskName,jdbcType=VARCHAR}");
        }
        
        if (record.getTaskParam() != null) {
            sql.VALUES("TASK_PARAM", "#{taskParam,jdbcType=VARCHAR}");
        }
        
        if (record.getTaskUrl() != null) {
            sql.VALUES("TASK_URL", "#{taskUrl,jdbcType=VARCHAR}");
        }
        
        if (record.getTaskHeader() != null) {
            sql.VALUES("TASK_HEADER", "#{taskHeader,jdbcType=VARCHAR}");
        }
        
        if (record.getActuatorCode() != null) {
            sql.VALUES("ACTUATOR_CODE", "#{actuatorCode,jdbcType=VARCHAR}");
        }
        
        if (record.getActuatorType() != null) {
            sql.VALUES("ACTUATOR_TYPE", "#{actuatorType,jdbcType=VARCHAR}");
        }
        
        if (record.getRetryNum() != null) {
            sql.VALUES("RETRY_NUM", "#{retryNum,jdbcType=INTEGER}");
        }
        
        if (record.getBatchNo() != null) {
            sql.VALUES("BATCH_NO", "#{batchNo,jdbcType=VARCHAR}");
        }
        
        if (record.getIpAddress() != null) {
            sql.VALUES("IP_ADDRESS", "#{ipAddress,jdbcType=VARCHAR}");
        }
        
        if (record.getInput() != null) {
            sql.VALUES("INPUT", "#{input,jdbcType=VARCHAR}");
        }
        
        if (record.getOutput() != null) {
            sql.VALUES("OUTPUT", "#{output,jdbcType=VARCHAR}");
        }
        
        if (record.getResult() != null) {
            sql.VALUES("RESULT", "#{result,jdbcType=BIT}");
        }
        
        if (record.getException() != null) {
            sql.VALUES("EXCEPTION", "#{exception,jdbcType=VARCHAR}");
        }
        
        if (record.getNextTime() != null) {
            sql.VALUES("NEXT_TIME", "#{nextTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getExecuteTime() != null) {
            sql.VALUES("EXECUTE_TIME", "#{executeTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getRemark() != null) {
            sql.VALUES("REMARK", "#{remark,jdbcType=LONGVARCHAR}");
        }
        
        return sql.toString();
    }

    public String updateByPrimaryKeySelective(TaskResult record) {
        SQL sql = new SQL();
        sql.UPDATE("t_task_result");
        
        if (record.getTaskCode() != null) {
            sql.SET("TASK_CODE = #{taskCode,jdbcType=VARCHAR}");
        }
        
        if (record.getTaskName() != null) {
            sql.SET("TASK_NAME = #{taskName,jdbcType=VARCHAR}");
        }
        
        if (record.getTaskParam() != null) {
            sql.SET("TASK_PARAM = #{taskParam,jdbcType=VARCHAR}");
        }
        
        if (record.getTaskUrl() != null) {
            sql.SET("TASK_URL = #{taskUrl,jdbcType=VARCHAR}");
        }
        
        if (record.getTaskHeader() != null) {
            sql.SET("TASK_HEADER = #{taskHeader,jdbcType=VARCHAR}");
        }
        
        if (record.getActuatorCode() != null) {
            sql.SET("ACTUATOR_CODE = #{actuatorCode,jdbcType=VARCHAR}");
        }
        
        if (record.getActuatorType() != null) {
            sql.SET("ACTUATOR_TYPE = #{actuatorType,jdbcType=VARCHAR}");
        }
        
        if (record.getRetryNum() != null) {
            sql.SET("RETRY_NUM = #{retryNum,jdbcType=INTEGER}");
        }
        
        if (record.getBatchNo() != null) {
            sql.SET("BATCH_NO = #{batchNo,jdbcType=VARCHAR}");
        }
        
        if (record.getIpAddress() != null) {
            sql.SET("IP_ADDRESS = #{ipAddress,jdbcType=VARCHAR}");
        }
        
        if (record.getInput() != null) {
            sql.SET("INPUT = #{input,jdbcType=VARCHAR}");
        }
        
        if (record.getOutput() != null) {
            sql.SET("OUTPUT = #{output,jdbcType=VARCHAR}");
        }
        
        if (record.getResult() != null) {
            sql.SET("RESULT = #{result,jdbcType=BIT}");
        }
        
        if (record.getException() != null) {
            sql.SET("EXCEPTION = #{exception,jdbcType=VARCHAR}");
        }
        
        if (record.getNextTime() != null) {
            sql.SET("NEXT_TIME = #{nextTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getExecuteTime() != null) {
            sql.SET("EXECUTE_TIME = #{executeTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getRemark() != null) {
            sql.SET("REMARK = #{remark,jdbcType=LONGVARCHAR}");
        }
        
        sql.WHERE("ID = #{id,jdbcType=INTEGER}");
        
        return sql.toString();
    }
    
    public String queryTaskResultByParam(TaskResult record) {
    	SQL sql = new SQL();
    	sql.SELECT("t.*");
    	sql.FROM("t_task_result t");
    	if(!StringUtils.isEmpty(record.getTaskCode())) {
    		sql.WHERE("t.task_code like CONCAT('%',#{taskCode,jdbcType=VARCHAR},'%') ");
    	}
    	
    	if(!StringUtils.isEmpty(record.getTaskName())) {
    		sql.WHERE("t.task_name like CONCAT('%',#{taskName,jdbcType=VARCHAR},'%') ");
    	}
    	
    	if(!StringUtils.isEmpty(record.getBatchNo())) {
    		sql.WHERE("t.batch_no like CONCAT('%',#{batchNo,jdbcType=VARCHAR},'%') ");
    	}
    	sql.ORDER_BY("t.id desc");
    	return sql.toString();
    }
    
}